package com.cqeec.myblog.controller;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqeec.myblog.model.Album;
import com.cqeec.myblog.model.Blogger;
import com.cqeec.myblog.service.AlbumService;
import com.cqeec.myblog.service.AlbumServiceImpl;
import com.cqeec.myblog.util.JsonUtils;

/**
 * Servlet implementation class AlbumServlet
 */
@WebServlet("/albumServlet")
public class AlbumServlet extends HttpServlet {
	
	private AlbumService albumService=new AlbumServiceImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		 String operation=request.getParameter("operation");
		 
		 
         if("albumPage".equals(operation)){
        	 
        	 Blogger blogger=(Blogger)request.getSession().getAttribute("blogger");
        	 String bid=blogger.getId();
        	 List<Album> list=albumService.findAlbumByBid(bid);
        	 
        	 request.setAttribute("albums", list);
        	 
        	 request.getRequestDispatcher("/album.jsp").forward(request, response);
         }
         
         if("deleteById".equals(operation)) {
        	  response.setContentType("text/json; utf-8");
        	  String aid=request.getParameter("aid");
        	  albumService.removeById(aid);
        	  response.getWriter().write("{\"flag\":\"true\"}");
         }
         
         if("addAlbum".equals(operation)) {
        	 request.setCharacterEncoding("utf-8");
        	 response.setContentType("text/json; utf-8");
        	 String title=request.getParameter("title");
        	 String describ=request.getParameter("describ");
        	 Blogger blogger=(Blogger) request.getSession().getAttribute("blogger");
        	 String bid=blogger.getId();
        	 albumService.add(title,describ,bid);
        	 response.getWriter().write("{\"flag\":\"true\"}");
         }
         
         if("updateAlbum".equals(operation)) {
        	 request.setCharacterEncoding("utf-8");
        	 response.setContentType("text/json; utf-8");
        	 String id=request.getParameter("album_id");
        	 String title=request.getParameter("album_title");
        	 String describ=request.getParameter("album_desc");
        	 albumService.updateAlbum(id,title,describ);
        	 response.getWriter().write("{\"flag\":\"true\"}");
         }
         if("album_describ".equals(operation)) {
        	 String id=request.getParameter("album_id");
        	 //根据相册id查找相应数据，并转发至相册详情页面
        	 Album album=albumService.selectAlbumById(id);
        	 request.setAttribute("album",album);
        	 request.getRequestDispatcher("/albumDesc.jsp").forward(request, response);
         }
         if("addImages".equals(operation)) {
        	 String images_str=request.getParameter("images");
        	 String id=request.getParameter("albumId");
        	 Album album=albumService.selectAlbumById(id);
        	 String[] images_server=album.getImages();
        	 String[] images_client=images_str.split(",");
        	 String[] realImages=new String[images_server.length+images_client.length];
        	 System.arraycopy(images_server, 0, realImages, 0, images_server.length);
        	 System.arraycopy(images_client, 0, realImages, images_server.length, images_client.length);
        	 album.setImages(realImages);
        	 albumService.updateImages(album);
        	 response.getWriter().write("{\"flag\":\"true\"}");
         }
         if("deleteImage".equals(operation)) {
        	String albumId=request.getParameter("albumId");
        	String imgPath=request.getParameter("imgPath");
        	Album album=albumService.selectAlbumById(albumId);
        	String[] images=album.getImages();
        	String[] realImages=new String[images.length-1];
        	int count=0;
        	for(int i=0;i<images.length;i++) {
        		if(!images[i].equals(imgPath)) {
        			realImages[count]=images[i];
        			count++;
        		}
        	}
        	album.setImages(realImages);
        	albumService.updateImages(album);
        	response.getWriter().write("{\"flag\":\"true\"}");
         }
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
	}

}
